
	
	
	   ***data paths 
  
   global posted /Volumes/Sans titre/DATA_MICROEU/Posting
   global data /Users/Mathilde/Dropbox/Posted_workers/Data
   
   
   
   **output paths
  global replication_outputs_a /Users/Mathilde/Dropbox/Posted_workers/Replication/TNT_final/Replication_package/Appendix_figures
  global replication_estimates /Users/Mathilde/Dropbox/Posted_workers/Replication/TNT_final/Replication_package/Main_estimates
  global replication_estimates_a /Users/Mathilde/Dropbox/Posted_workers/Replication/TNT_final/Replication_package/Appendix_estimates
  global replication_outputs /Users/Mathilde/Dropbox/Posted_workers/Replication/TNT_final/Replication_package/Main_figures

  global frontalier 1 2 4 5 6 8 9 25 31 39 54 55 57 59 64 65 66 67 68 71 74 90

	
	
*****************************************************************************************
* Replication for Figure 5 and Table 1 + related appendix figures (appendix D)
****************************************************************************************


******************************************
* Raw data on province-level 
*****************************************

*Population 

forval y=1989/2015{
import excel "$data/Demographics/pop_departement.xls", sheet("`y'") firstrow clear
keep Estimationdepopulationau1er B D E
drop if missing(B)
rename Estimationdepopulationau1er code_insee
rename B departement
rename D cat1
rename E cat2
destring cat1, replace
destring cat2, replace
egen pop=rowtotal(cat1 cat2)
drop cat1 cat2
g year=`y'
drop if code_insee=="2A" 
drop if code_insee=="2B"
destring code_insee, replace
save "$data/Demographics/pop`y'.dta", replace
}

use "$data/Demographics/pop1989.dta", clear
forval y=1990/2015{
append using "$data/Demographics/pop`y'.dta"
}
save "$data/panelpop_19892015.dta", replace


*French employment at the province-nace level 

foreach y in TAZ TBE TFZ TGUIZ TGUMNNZ TGURURZ TOQ {
import excel "$data/Employment_France/INSEEACOSS/T202.xls", sheet("E - `y'") cellrange(A5:AD125) firstrow clear
sort year 
g flag=_n
drop if flag>101
drop if flag==1 
gen code_insee=substr(year,1,2)
drop if code_insee=="2A"
drop if code_insee=="2B"
destring code_insee, replace
rename year dept
drop dept
drop if code_insee>95
reshape long n, i(code_insee) j(year)
g sector="`y'"
save "$data/Employment_France/INSEEACOSS/raw`y'.dta", replace
}




foreach y in TGUO{
import excel "/$data/Employment_France/INSEEACOSS/T202.xls", sheet("E - `y'") firstrow clear
 sort year 
g flag=_n
drop if flag>101
drop if flag==1 
gen code_insee=substr(year,1,2)
drop if code_insee=="2A"
drop if code_insee=="2B"
destring code_insee, replace
rename year dept
drop dept
drop if code_insee>95
reshape long n, i(code_insee) j(year)
g sector="`y'"
save "$data/Employment_France/INSEEACOSS/raw`y'.dta", replace
}




use "$data/Employment_France/INSEEACOSS/rawTGUO.dta", clear 
foreach y in TAZ TBE TFZ TGUIZ TGUMNNZ TGURURZ TOQ {
append using "$data/Employment_France/INSEEACOSS/raw`y'.dta"
}

rename sector oldsector
g sector="."
replace sector="agriculture" if oldsector=="TAZ"
replace sector="construction" if oldsector=="TFZ"
replace sector="industrie" if oldsector=="TBE"
replace sector="hcr" if oldsector=="TGUIZ"
replace sector="spectacle" if oldsector=="TGURURZ"
replace sector="ett" if oldsector=="TGUMNNZ" 
replace sector="otherservices" if oldsector=="TGUO"
replace sector="nonmarchand" if oldsector=="TOQ" 
rename n employment
save "$data/raw_employmentINSEE_19892017.dta", replace

******************************************
* Data on received posting forms "DPD"
*****************************************

*Initial region-level posting inflows (historical DPD report by region pre systematic collection)

import excel "$data/DPD_2003_numerized.xlsx", sheet("Sheet1") firstrow clear
save "$data/numerized_2003dpd.dta", replace 

*DPD database at the sector-province-year level 2005-2015
*Merge DPD and domestic employment database 

    import excel "$data/DPD_sectoral_province20052015.xls", sheet("sectoral") firstrow clear
	rename Departement code_insee
	rename secteur sector
	rename Annee year
	keep if year<2016
	replace code_insee=code_insee/10 if code_insee>9 
	drop if code_insee>95
	*for minor nb of observations (2%), the raw files provided by the french ministry
	*have two lines per sector-province-year but the number of posted workers differ 
	*so those are not duplicates but were given in two lines instead of one-- need to aggregate those lines: 
	collapse (sum) nbsal, by(year code_insee sector Region)
	preserve 
	drop if code_insee>95
	g false=0
	collapse (mean) false, by(code_insee Region)
	save "$data/key_regionprovince.dta", replace 
	restore
	merge 1:1 code_insee year sector using "$data/raw_employmentINSEE_19892017.dta"
	drop if year>2015
	*create a balance panel for years with no posting recorded by French gvt 
	replace nbsal=0 if missing(nbsal)
    drop _m
	merge m:1 code_insee year  using  "$data/panelpop_19892015.dta"
	keep if _m==3
	drop if missing(employment)
	cap drop _m
	save "$data/merge_employment_FR_19892015.dta", replace
	************bbaseline panel data on domestic employment by province-sector and info on posting imports at the same levels
	
	*******************************************
	*Build initial exposure to posting 
	*use employment share to allocate initial posting inflows to provinces 
	
	use "$data/merge_employment_FR_19892015.dta", clear 
	drop if year>2015
	*exposed sectors: non zero posted workers by sector 
	egen totnbsal=sum(nbsal), by(sector)
	g postable=1
	replace postable=0 if totnbsal==0
	*pre-reform employment shares
	keep if year==2003 & postable==1 
	egen totemp=sum(employment), by(code_insee)
	collapse (sum) employment (mean) totemp, by(code_insee Region)
	drop Region
	merge m:1 code_insee using "$data/key_regionprovince.dta"
	drop if _m==2
	drop _m
	merge m:1 Region using "$data/numerized_2003dpd.dta"
	drop if _m==2	
	egen reg_emp=sum(employment), by(Region)
	g s_reg=employment/reg_emp
	g posted=nbsal2003*s_reg
	g exposure=posted/totemp
	egen rankrelemp=xtile(exposure), nq(10)
	drop nbsal2003
	rename posted nbsal2003
	save "$data/initialexposure.dta", replace
	   
	   *census micro data SAPHIRE, share of foreigners in pre-reform census year
	   use "$data/rp_panel.dta", clear 
	   destring an_recens, replace
	   drop if dep_res_18=="2A"
	   drop if dep_res_18=="2B"
	   destring dep_res_18, replace
	   keep if an_recens==1999
	   collapse (sum) pond, by(for dep_res_18)
	   reshape wide pond, i(dep_res_18) j(for)
	   g s_mig_alt=pond0/(pond1+pond0)*100
	   rename dep_res_18 code_insee
	   save "$data/census99_mig.dta", replace
 
	
	*************************regression analysis for Figure 5 and Table 1********************************
	
	use "$data/merge_employment_FR_19892015.dta", clear 
	drop if year>2015
	*exposed sectors 
	egen totnbsal=sum(nbsal), by(sector)
	g postable=1
	replace postable=0 if totnbsal==0
	*total employment 
	egen totemp=sum(employment), by(code_insee year)
    *employment in postable industries
	egen totemp_p=sum(employment) if postable==1, by(code_insee year) 
	*shares 
	g s_industrie=employment/totemp if sector=="industrie"
    g s_postable=totemp_p/totemp
	
    *collapse at the exposed dummy-province-year level 
	collapse (sum) employment nbsal (mean) totemp pop (min) s_postable s_industrie, by(code_insee year postable)
	
	*create 2003 variables to use as controls in the regression analysis
	foreach y in pop s_ind s_postable totemp{
    g `y'd=`y' if year==2003
	egen `y'2003=min(`y'd), by(code_insee)
	drop `y'd
	}
		
	g emprel=employment/pop*100
	
	*Observed exposure after the reform 
	egen nbsalmeand=mean(nbsal) if year>2004 & postable==1, by(code_insee)
	egen nbsalmean=min(nbsalmeand), by(code_insee)
	gen exposure_post_pp=(nbsalmean/totemp2003)*100
	*constant variable at the province-level
	
	
	*actual exposure (computed only when inflows are not zero after 2004)
	egen nbsalmeand2=mean(nbsal) if year>2004 &nbsal!=0 & postable==1, by(code_insee)
	egen nbsalmean2=min(nbsalmeand2), by(code_insee)
	gen exposure_post_pp2=(nbsalmean2/totemp2003)*100


	*merge historical data on posted workers in 2003 
	merge m:1 code_insee using "$data/initialexposure.dta", keepusing(nbsal2003 Region)
	drop _m 
	
	*constant variable at the province-level
	gen exposure_pre_pp=(nbsal2003/totemp2003)*100
	
	*merge INSEE census data on blue-collar workers on pre-reform census (1999) -- publicly available
    merge m:1 code_insee using "$data/structure_dep.dta", keepusing(csx_rec6rpop1999)
    g s_ouvrier=csx_rec6rpop1999/totemp2003
    drop _m 
	
	*merge trade data 
	*see do-file appendix_d3 for creation of this variable
    merge m:1 code_insee using "$data/tradeshock.dta"
    g s_nms=shock/totemp2003
	drop _m
	
	*merge unemp 
	merge m:1 code_insee year using "$data/chomage_mean.dta"
	cap drop if _m==2
	g chomagenb=((chomage/100)*totemp)/(1-(chomage/100))
	g chomrel=(chomagenb/pop)*100
	rename chomage T1
	
	foreach y in chomrel{
    g `y'd=`y' if year==2003
	egen `y'2003=min(`y'd), by(code_insee)
	drop `y'd
	}
	
	
	drop _m
	merge m:1 code_insee using "$data/nms_departement_main.dta", keepusing(distancenms2)
	g exposure_distance=-distancenms2/1000
	drop _m
	*merge SAPHIR census data on foreign population (1999)
	merge m:1 code_insee using "$data/census99_mig.dta", keepusing(s_mig*)
	drop _m 
	rename s_mig inpop
	
	*drop years outside the DPD dataset 
	drop if year>2015
	
	g frontalier=0
	foreach y in $frontalier{
	replace frontalier=1 if code_insee==`y'
	}
	

	foreach y in totemp employment emprel exposure_pre_pp exposure_post_pp pop exposure_post_pp2{
		g log`y'=log(`y')
	}

	reg logexposure_post_pp logexposure_pre_pp if year==2003 & postable==1 [aweight=pop2003], ro
	tabulate year, generate(g)
	egen cl=group(Region)


	preserve 
	forval y=1990/2015{
	g g`y'=0 
	replace g`y'=1  if year==`y'
	replace g`y'=g`y'*exposure_pre_pp
	label var g`y' "`y'"
	}
	g zero=1
	label var zero "2003"
	
	
	reghdfe logemployment g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee  year) cluster(code_insee)
	est store baseline 
	reghdfe logemployment g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year year) cluster(code_insee)
	est store manuf
	reghdfe logemployment g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_postable2003##year year) cluster(code_insee)
	est store exposed
	reghdfe logemployment g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year c.s_postable2003##year year c.s_ouvrier##year) cluster(code_insee)
	est store ouvrier
	reghdfe logemployment g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year c.s_postable2003##year year c.s_ouvrier##year c.inpop##year) cluster(code_insee)
	est store  mig
	reghdfe logemployment g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year c.s_postable2003##year year c.s_ouvrier##year c.inpop##year c.s_nms##year) cluster(code_insee)
	est store nms
	reghdfe logemployment g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 , absorb(code_insee c.s_ind2003##year c.s_postable2003##year year c.s_ouvrier##year c.inpop##year) cluster(code_insee)
	est store uw
    reghdfe logemployment g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year c.s_postable2003##year c.s_ouvrier##year  frontalier##i.year year) cluster(code_insee)
	est store frontalier
	reghdfe logemployment g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year c.s_postable2003##year c.s_ouvrier##year  c.chomrel2003##i.year year) cluster(code_insee)
	est store unemp
	reghdfe logemployment g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee  year) cluster(cl)
	est store cl


		
	coefplot (baseline, label("Baseline (Province, Year FE)") ) ///
	(manuf, label("+ 2003 Share of ManufacturingxYear FE") lcolor(gs10) mcolor(gs10) ciopts(lcolor(gs10)) msymbol(T)) ///
	(exposed, label("+ 2003 Share of Exposed SectorsxYear FE") lcolor(blue) mcolor(blue) ciopts(lcolor(blue)) msymbol(X)) ///
	(ouvrier, label("+ 2003 Share of Blue collar workersxYear FE") lcolor(eltblue) mcolor(eltblue) ciopts(lcolor(eltblue)) msymbol(S)) ///
	(mig, label("+ 2003 Share of ForeignersxYear FE") lcolor(eltblue) mcolor(ebblue) ciopts(lcolor(ebblue)) msymbol(Oh)) ///
	 , keep( g1994 g1995 g1996 g1997 g1998 g1999 g2000 g2001 g2002 zero g2004 g2005 g2006 g2007 g2008 g2009 g2010 g2011 g2012 g2013 g2014     g2015) vertical omit ///
	graphregion(fcolor(white) lcolor(white)) coeflabels(,angle(45)) yline(0, lcolor(gs10) lpattern(dash)) ///
	 xline(10.5, lcolor(red)) recast(connected) ///
	 ytitle("Coeff on Year*Exposure Index") legend(cols(1) ring(0) pos(2) size(small)) 	ylabel(-.2 (.1) .15)
     graph export "$replication_outputs/Figure5a.pdf", replace
		
	
	reghdfe logemployment g1994-g2002 zero g2004-g2015 if year>1993 & postable==0 [aweight=pop2003], absorb(code_insee  year year##c.s_postable2003 year##c.s_ind2003 year##c.s_ouvrier) cluster(code_insee)
    est store sheltered

    reghdfe logpop g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee  year year##c.s_postable2003 year##c.s_ind2003  year##c.s_ouvrier) cluster(code_insee)
    est store pop

 
	coefplot (baseline, label("Baseline (Province, Year FE)") ) ///
	(manuf, label("+2003 Share of ManufacturingxYear FE") lcolor(gs10) mcolor(gs10) ciopts(lcolor(gs10))) ///
	(exposed, label("+2003 Share of Exposed SectorsxYear FE") lcolor(blue) mcolor(blue) ciopts(lcolor(blue))) ///
		(pop, label("Log Population") lcolor(red) mcolor(red) ciopts(lcolor(red)) lpattern(dash)) ///
						(sheltered, label("Log Employment in Sheltered Sectors") msymbol(T) lcolor(orange) mcolor(orange) ciopts(lcolor(orange)) ) ///
	 , keep( g1994 g1995 g1996 g1997 g1998 g1999 g2000 g2001 g2002 zero g2004 g2005 g2006 g2007 g2008 g2009 g2010 g2011 g2012 g2013 g2014 g2015) vertical omit ///
	graphregion(fcolor(white) lcolor(white)) coeflabels(,angle(45)) yline(0, lcolor(gs10) lpattern(dash)) xline(10.5) recast(connected) ///
	ytitle("Coeff on Year*Exposure Index") legend(cols(1) ring(0) pos(2) size(small) ) 	ylabel(-.2 (.1) .15)
	graph export "$replication_outputs/Figure5b.pdf", replace
		
	
	
	*Some robustness 
	
	coefplot (baseline, label("Baseline (Province, Year FE)") ) ///
	(manuf, label("All controlsxYear FE") lcolor(eltblue) mcolor(ebblue) ciopts(lcolor(ebblue)) msymbol(Oh)) ///
		(unemp, label("+ 2003 Unemployment RatexYear FE") lcolor(eltblue) mcolor(ebblue) ciopts(lcolor(ebblue))) ///
	 , keep( g1994 g1995 g1996 g1997 g1998 g1999 g2000 g2001 g2002 zero g2004 g2005 g2006 g2007 g2008 g2009 g2010 g2011 g2012 g2013 g2014     g2015) vertical omit ///
	graphregion(fcolor(white) lcolor(white)) coeflabels(,angle(45)) yline(0, lcolor(gs10) lpattern(dash)) ///
	 xline(10.5, lcolor(red)) recast(connected) ///
	 ytitle("Coeff on Year*Exposure Index") legend(cols(1) ring(0) pos(2) size(small)) 	ylabel(-.2 (.1) .15)
     graph export "$replication_outputs_a/FigureD43a.pdf", replace

		coefplot (baseline, label("Baseline (Province, Year FE)") ) ///
	(manuf, label("All controlsxYear FE") lcolor(eltblue) mcolor(ebblue) ciopts(lcolor(ebblue)) msymbol(Oh)) ///
		(frontalier, label("+ BorderxYear FE") lcolor(eltblue) mcolor(ebblue) ciopts(lcolor(ebblue))) ///
	 , keep( g1994 g1995 g1996 g1997 g1998 g1999 g2000 g2001 g2002 zero g2004 g2005 g2006 g2007 g2008 g2009 g2010 g2011 g2012 g2013 g2014     g2015) vertical omit ///
	graphregion(fcolor(white) lcolor(white)) coeflabels(,angle(45)) yline(0, lcolor(gs10) lpattern(dash)) ///
	 xline(10.5, lcolor(red)) recast(connected) ///
	 ytitle("Coeff on Year*Exposure Index") legend(cols(1) ring(0) pos(2) size(small)) 	ylabel(-.2 (.1) .15)
     graph export "$replication_outputs_a/FigureD43b.pdf", replace
		
		
	coefplot (baseline, label("Baseline (Province, Year FE)") ) ///
	(manuf, label("2003 Share of ManufacturingxYear FE") lcolor(eltblue) mcolor(ebblue) ciopts(lcolor(ebblue)) msymbol(Oh)) ///
		(nms, label("+ Exposure to NMS ImportsxYear FE") lcolor(eltblue) mcolor(ebblue) ciopts(lcolor(ebblue))) ///
	 , keep( g1994 g1995 g1996 g1997 g1998 g1999 g2000 g2001 g2002 zero g2004 g2005 g2006 g2007 g2008 g2009 g2010 g2011 g2012 g2013 g2014     g2015) vertical omit ///
	graphregion(fcolor(white) lcolor(white)) coeflabels(,angle(45)) yline(0, lcolor(gs10) lpattern(dash)) ///
	 xline(10.5, lcolor(red)) recast(connected) ///
	 ytitle("Coeff on Year*Exposure Index") legend(cols(1) ring(0) pos(2) size(small)) 	ylabel(-.2 (.1) .15)
     graph export "$replication_outputs/FigureD43c.pdf", replace
		
		
	coefplot (mig, label("weighted by initial pop") ) ///
	(uw, label("unweighted")) ///
	 , keep( g1994 g1995 g1996 g1997 g1998 g1999 g2000 g2001 g2002 zero g2004 g2005 g2006 g2007 g2008 g2009 g2010 g2011 g2012 g2013 g2014     g2015) vertical omit ///
	graphregion(fcolor(white) lcolor(white)) coeflabels(,angle(45)) yline(0, lcolor(gs10) lpattern(dash)) ///
	 xline(10.5, lcolor(red)) recast(connected) ///
	 ytitle("Coeff on Year*Exposure Index") legend(cols(1) ring(0) pos(2) size(small)) 	ylabel(-.2 (.1) .15)
    graph export "$replication_outputs_a/FigureD44.pdf", replace
	
	coefplot cl ///
	 , keep( g1994 g1995 g1996 g1997 g1998 g1999 g2000 g2001 g2002 zero g2004 g2005 g2006 g2007 g2008 g2009 g2010 g2011 g2012 g2013 g2014     g2015) vertical omit ///
	graphregion(fcolor(white) lcolor(white)) coeflabels(,angle(45)) yline(0, lcolor(gs10) lpattern(dash)) ///
	 xline(10.5, lcolor(red)) recast(connected) ///
	 ytitle("Coeff on Year*Exposure Index") 
     graph export "$replication_outputs_a/FigureD43d.pdf", replace
	 

	 foreach y in emprel chomrel{
	reghdfe `y' g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee  year) cluster(code_insee)
	est store `y'baseline 
	reghdfe `y' g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year year) cluster(code_insee)
	est store `y'manuf
	reghdfe `y' g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year c.s_postable2003##year year) cluster(code_insee)
	est store `y'exposed
	reghdfe `y' g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year c.s_postable2003##year year c.s_ouvrier##year) cluster(code_insee)
	est store `y'ouvrier
	reghdfe `y' g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year c.s_postable2003##year year c.s_ouvrier##year c.inpop##year) cluster(code_insee)
	est store `y'mig
	reghdfe `y' g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year c.s_postable2003##year year c.s_ouvrier##year c.inpop##year c.s_nms##year) cluster(code_insee)
	est store `y'nms
	reghdfe `y' g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 , absorb(code_insee c.s_ind2003##year c.s_postable2003##year year c.s_ouvrier##year c.inpop##year c.s_nms##year) cluster(code_insee)
	est store `y'uw
	 }
	 
	
	coefplot (emprelbaseline, label("EmploymentPop")) ///
	(emprelmanuf, label("") lcolor(gs10) mcolor(gs10) ciopts(lcolor(gs10))) ///
	(emprelexposed, label("") lcolor(blue) mcolor(blue) ciopts(lcolor(blue))) ///
	(emprelouvrier, label("") lcolor(eltblue) mcolor(eltblue) ciopts(lcolor(eltblue))) ///
	(emprelmig, label("") lcolor(eltblue) mcolor(ebblue) ciopts(lcolor(ebblue)) msymbol(o)) ///
	(emprelnms, label("") lcolor(blue) mcolor(blue) ciopts(lcolor(blue)) msymbol(o) lpattern(dash)) ///
	(empreluw, label("") lcolor(pink) mcolor(pink) ciopts(lcolor(pink)) msymbol(o)) ///
	(chomrelbaseline, label("UnemploymentPop") lcolor(dknavy) mcolor(dknavy) ciopts(lcolor(dknavy)) msymbol(t)) ///
	(chomrelmanuf, label("") lcolor(gs10) mcolor(gs10) ciopts(lcolor(gs10)) msymbol(t)) ///
	(chomrelexposed, label("") lcolor(blue) mcolor(blue) ciopts(lcolor(blue)) msymbol(t)) ///
	(chomrelouvrier, label("") lcolor(eltblue) mcolor(eltblue) ciopts(lcolor(eltblue)) msymbol(t)) ///
	(chomrelmig, label("") lcolor(eltblue) mcolor(ebblue) ciopts(lcolor(ebblue)) msymbol(t)) ///
	(chomrelnms, label("") lcolor(blue) mcolor(blue) ciopts(lcolor(blue)) msymbol(t) lpattern(dash)) ///
	(chomreluw, label("") lcolor(pink) mcolor(pink) ciopts(lcolor(pink)) msymbol(t) lpattern(dash)) ///
	 , keep( g1994 g1995 g1996 g1997 g1998 g1999 g2000 g2001 g2002 zero g2004 g2005 g2006 g2007 g2008 g2009 g2010 g2011 g2012 g2013 g2014     g2015) vertical omit ///
	graphregion(fcolor(white) lcolor(white)) coeflabels(,angle(45)) yline(0, lcolor(gs10) lpattern(dash)) ///
	 xline(10.5, lcolor(red)) recast(connected) ///
	 ytitle("Coeff on Year*Exposure Index")  legend(order(2  16))	ylabel(-5 (1) 1)
	 graph export "$replication_outputs_a/FigureD42.pdf", replace	
		  
reghdfe emprel g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee  year) cluster(code_insee)
	est store baseline 
	reghdfe emprel g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year year) cluster(code_insee)
	est store manuf
	reghdfe emprel g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_postable2003##year year) cluster(code_insee)
	est store exposed
	reghdfe emprel g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year c.s_postable2003##year year c.s_ouvrier##year) cluster(code_insee)
	est store ouvrier
	reghdfe emprel g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year c.s_postable2003##year year c.s_ouvrier##year c.inpop##year) cluster(code_insee)
	est store  mig
	

		
	coefplot (baseline, label("Baseline (Province, Year FE)") ) ///
	(manuf, label("+ 2003 Share of ManufacturingxYear FE") lcolor(gs10) mcolor(gs10) ciopts(lcolor(gs10)) msymbol(T)) ///
	(exposed, label("+ 2003 Share of Exposed SectorsxYear FE") lcolor(blue) mcolor(blue) ciopts(lcolor(blue)) msymbol(X)) ///
	(ouvrier, label("+ 2003 Share of Blue collar workersxYear FE") lcolor(eltblue) mcolor(eltblue) ciopts(lcolor(eltblue)) msymbol(S)) ///
	(mig, label("+ 2003 Share of ForeignersxYear FE") lcolor(eltblue) mcolor(ebblue) ciopts(lcolor(ebblue)) msymbol(Oh)) ///
	 , keep( g1994 g1995 g1996 g1997 g1998 g1999 g2000 g2001 g2002 zero g2004 g2005 g2006 g2007 g2008 g2009 g2010 g2011 g2012 g2013 g2014     g2015) vertical omit ///
	graphregion(fcolor(white) lcolor(white)) coeflabels(,angle(45)) yline(0, lcolor(gs10) lpattern(dash)) ///
	 xline(10.5, lcolor(red)) recast(connected) ///
	 ytitle("Coeff on Year*Exposure Index") legend(cols(1) ring(0) pos(7) size(small)) 	ylabel(-3 (1) 1)
     graph export "$replication_outputs/FigureD41.pdf", replace
	 
	 
	 reghdfe logtotemp g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee  year c.s_ind2003##year c.s_postable2003##year year c.s_ouvrier##year c.inpop##year) cluster(code_insee)
	 est store baseline 
	coefplot baseline ///
	 , keep( g1994 g1995 g1996 g1997 g1998 g1999 g2000 g2001 g2002 zero g2004 g2005 g2006 g2007 g2008 g2009 g2010 g2011 g2012 g2013 g2014     g2015) vertical omit ///
	graphregion(fcolor(white) lcolor(white)) coeflabels(,angle(45)) yline(0, lcolor(gs10) lpattern(dash)) ///
	 xline(10.5, lcolor(red)) recast(connected) ///
	 ytitle("Coeff on Year*Exposure Index")  	ylabel(-.1 (.1) .15)
     graph export "$replication_outputs_a/FigureD38.pdf", replace
	 
	 
	 reghdfe logtotemp g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee  year) cluster(code_insee)
	est store baseline 
	reghdfe logtotemp g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year year) cluster(code_insee)
	est store manuf
	reghdfe logtotemp g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_postable2003##year year) cluster(code_insee)
	est store exposed
	reghdfe logtotemp g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year c.s_postable2003##year year c.s_ouvrier##year) cluster(code_insee)
	est store ouvrier
	reghdfe logtotemp g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year c.s_postable2003##year year c.s_ouvrier##year c.inpop##year) cluster(code_insee)
	est store  mig
	coefplot (baseline, label("Baseline (Province, Year FE)") ) ///
	(manuf, label("+ 2003 Share of ManufacturingxYear FE") lcolor(gs10) mcolor(gs10) ciopts(lcolor(gs10)) msymbol(T)) ///
	(exposed, label("+ 2003 Share of Exposed SectorsxYear FE") lcolor(blue) mcolor(blue) ciopts(lcolor(blue)) msymbol(X)) ///
	(ouvrier, label("+ 2003 Share of Blue collar workersxYear FE") lcolor(eltblue) mcolor(eltblue) ciopts(lcolor(eltblue)) msymbol(S)) ///
	(mig, label("+ 2003 Share of ForeignersxYear FE") lcolor(eltblue) mcolor(ebblue) ciopts(lcolor(ebblue)) msymbol(Oh)) ///
	 , keep( g1994 g1995 g1996 g1997 g1998 g1999 g2000 g2001 g2002 zero g2004 g2005 g2006 g2007 g2008 g2009 g2010 g2011 g2012 g2013 g2014     g2015) vertical omit ///
	graphregion(fcolor(white) lcolor(white)) coeflabels(,angle(45)) yline(0, lcolor(gs10) lpattern(dash)) ///
	 xline(10.5, lcolor(red)) recast(connected) ///
	 ytitle("Coeff on Year*Exposure Index") legend(cols(1) ring(0) pos(2) size(small)) 	ylabel(-.1 (.1) .15)
     graph export "$replication_outputs_a/FigureD39.pdf", replace

	 
	 
	 
	 restore 
	 
	 
	 
	 
	 
	
	preserve 
	forval y=1990/2015{
	g g`y'=0 
	replace g`y'=1  if year==`y'
	replace g`y'=g`y'*exposure_post_pp
	label var g`y' "`y'"
	}
	g zero=1
	label var zero "2003"

	reghdfe logemployment g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee  year) cluster(code_insee)
	est store baseline 
	reghdfe logemployment g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year c.s_postable2003##year year c.s_ouvrier##year c.inpop##year) cluster(code_insee)
	est store  mig

	coefplot (baseline, label("Baseline (Province, Year FE)") ) ///
	(mig, label("All controlsxYear FE") lcolor(gs10) mcolor(gs10) ciopts(lcolor(gs10)) msymbol(T)) ///
	 , keep( g1994 g1995 g1996 g1997 g1998 g1999 g2000 g2001 g2002 zero g2004 g2005 g2006 g2007 g2008 g2009 g2010 g2011 g2012 g2013 g2014     g2015) vertical omit ///
	graphregion(fcolor(white) lcolor(white)) coeflabels(,angle(45)) yline(0, lcolor(gs10) lpattern(dash)) ///
	 xline(10.5, lcolor(red)) recast(connected) ///
	 ytitle("Coeff on Year*Exposure Index") legend(cols(1) ring(0) pos(2) size(small)) 	ylabel(-.1 (.04) .02)
     graph export "$replication_outputs_a/FigureD40a.pdf", replace
	 
	 
	reghdfe emprel g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee  year) cluster(code_insee)
	est store baseline 
	reghdfe emprel g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year c.s_postable2003##year year c.s_ouvrier##year c.inpop##year) cluster(code_insee)
	est store  mig

	coefplot (baseline, label("Baseline (Province, Year FE)") ) ///
	(mig, label("All controlsxYear FE") lcolor(gs10) mcolor(gs10) ciopts(lcolor(gs10)) msymbol(T)) ///
	 , keep( g1994 g1995 g1996 g1997 g1998 g1999 g2000 g2001 g2002 zero g2004 g2005 g2006 g2007 g2008 g2009 g2010 g2011 g2012 g2013 g2014     g2015) vertical omit ///
	graphregion(fcolor(white) lcolor(white)) coeflabels(,angle(45)) yline(0, lcolor(gs10) lpattern(dash)) ///
	 xline(10.5, lcolor(red)) recast(connected) ///
	 ytitle("Coeff on Year*Exposure Index") legend(cols(1) ring(0) pos(2) size(small)) 	ylabel(-2 (1) 1)
     graph export "$replication_outputs_a/FigureD40b.pdf", replace


	restore 
	
	
	
	

	preserve 
	forval y=1990/2015{
	g g`y'=0 
	replace g`y'=1  if year==`y'
	replace g`y'=g`y'*exposure_distance
	label var g`y' "`y'"
	}
	g zero=1
	label var zero "2003"

	

	reghdfe logemprel g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year c.s_postable2003##year year c.s_ouvrier##year c.inpop##year) cluster(code_insee)
	est store  popr
	reghdfe logemployment g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee c.s_ind2003##year c.s_postable2003##year year c.s_ouvrier##year c.inpop##year) cluster(code_insee)
	est store  emp
	reghdfe logemprel g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 , absorb(code_insee c.s_ind2003##year c.s_postable2003##year year c.s_ouvrier##year c.inpop##year) cluster(code_insee)
	est store  popruw
	
	coefplot ///
	(popr, label("Distance to NMSxYear FE, Pop ratio") ) ///
	(emp, label("Distance to NMSxYear FE, Emp") lcolor(eltblue) mcolor(eltblue) ciopts(lcolor(eltblue))) ///
	(popruw, label("Distance to NMSxYear FE, Emp") lcolor(pink) mcolor(pink) ciopts(lcolor(pink))) ///
	 , keep( g1994 g1995 g1996 g1997 g1998 g1999 g2000 g2001 g2002 zero g2004 g2005 g2006 g2007 g2008 g2009 g2010 g2011 g2012 g2013 g2014     g2015) vertical omit ///
	graphregion(fcolor(white) lcolor(white)) coeflabels(,angle(45)) yline(0, lcolor(gs10) lpattern(dash)) ///
	 xline(10.5, lcolor(red)) recast(connected) ///
	 ytitle("Coeff on Year*Distance/1000") legend(cols(1) ring(0) pos(2) size(small)) 	ylabel(-.06 (.02) .04)
     graph export "$replication_outputs_a/FigureD46.pdf", replace



	restore 
	
	 
**********************************************

	 
	preserve
	 
	 
	keep if postable==1
	g exposure=nbsal/totemp2003*100
	egen rank1=xtile(exposure_pre_pp) if year==2004, nq(10)
	egen rank2=xtile(exposure_post_pp) if year==2004, nq(10)
	egen rank_pre=min(rank1), by(code_insee)
	egen rank_post=min(rank2), by(code_insee)
	su exposure if year==2015 [aweight=pop2003], d 
	su exposure if year==2015 & rank_pre==10 [aweight=pop2003], d 
	su exposure_post_pp if year==2015 & rank_pre==10 [aweight=pop2003], d 
	su exposure if year==2015 & rank_pre<5 [aweight=pop2003], d 
	su exposure_post_pp if year==2015 & rank_post==10 [aweight=pop2003], d 
	*persistence in exposure 
	binscatter rank1 rank2, reportreg
	restore
	
	preserve 
	keep if postable==1
	egen rank1=xtile(exposure_pre_pp) if year==2004, nq(10)
	egen rank2=xtile(exposure_post_pp) if year==2004, nq(10)
	egen rank_pre=min(rank1), by(code_insee)
	egen rank_post=min(rank2), by(code_insee)
	collapse (sum) employment nbsal nbsal2003 totemp, by(postable rank_pre year)
	g exposure2=nbsal/totemp
	*replace exposure2=nbsal2003/totemp if year==2003
	replace exposure2=. if year==2004
	replace exposure2=. if year<2004
twoway  ///
	(connected exposure2 year if rank_pre==1) ///
(connected exposure2 year if rank_pre==10)  ///
(connected exposure2 year if rank_pre==9), graphregion(fcolor(white) lcolor(white)) ///
ytitle("Posted workers in total employment")  legend(label(1 "Bottom 10") label(2 "Top 10") label(3 "9th Decile") order(2 3 1)) xtitle("")
  graph export "$replication_outputs_a/FigureD30a.pdf", replace
	restore 
	
	
	preserve 
	keep if postable==1
	egen rank1=xtile(exposure_pre_pp) if year==2004, nq(10)
	egen rank2=xtile(exposure_post_pp) if year==2004, nq(10)
	egen rank_pre=min(rank1), by(code_insee)
	egen rank_post=min(rank2), by(code_insee)
	collapse (sum) employment nbsal nbsal2003 totemp, by(postable rank_post year)
	g exposure2=nbsal/totemp
	*replace exposure2=nbsal2003/totemp if year==2003
	replace exposure2=. if year==2004
	replace exposure2=. if year<2004
twoway  ///
	(connected exposure2 year if rank_post==1) ///
(connected exposure2 year if rank_post==10)  ///
(connected exposure2 year if rank_post==9), graphregion(fcolor(white) lcolor(white)) ///
ytitle("Posted workers in total employment")  legend(label(1 "Bottom 10") label(2 "Top 10") label(3 "9th Decile") order(2 3 1)) xtitle("")
  graph export "$replication_outputs_a/FigureD30b.pdf", replace
	restore 
	
	save "$data/panel_Figure5.dta", replace 
	
	use "$data/panel_Figure5.dta", clear
	keep if year==2003 | year==2015 | year==1993 | year==2000 | year==1999 | year==1990
	*keep only employment in exposed sectors
	keep if postable==1
	rename nbsal2003 posting_pre
	drop g*  log* s_postable s_industrie chomagenb nbsalmeand2
	
	reshape wide employment pop emprel totemp chomrel T1 nbsal* frontalier, i(code_insee ) j(year)	
	
	g deltadom20032015=(emprel2015-emprel2003)
	g deltachom20032015=(T12015-T12003)
	g deltachombis20032015=(chomrel2015-chomrel2003)
	
    g deltadom19932003=(emprel2003-emprel1993)
    g deltadom20002003=(emprel2003-emprel2000)


	reg deltadom20032015 exposure_post_pp [aweight=pop2003], ro 
	reg deltadom20032015 exposure_post_pp [aweight=pop2003], ro 
	
	foreach y in exposure_post_pp exposure_post_pp2 exposure_pre_pp{
	g log`y'=log(`y')	
	}
	g logchgemployment=(log(employment2015)-log(employment2003))*100
	g logchgtotemployment=(log(totemp2015)-log(totemp2003))*100
	g logchgpop=(log(pop2015)-log(pop2003))*100
	
	
	egen othershocks=sum(exposure_pre_pp)
	replace othershocks=othershocks-exposure_pre_pp
	g logother=log(othershocks)
	
	g lognbsal2003=log(posting_pre)
	g logemp2003=log(totemp2003)

	
	
	*OLS
	reg deltadom20032015 logexposure_post_pp [aweight=pop2003], ro first
	outreg2 using "$replication_estimates/Table1.xls", replace
	*Reduced Form
	reg deltadom20032015 logexposure_pre_pp [aweight=pop2003], ro first
	outreg2 using "$replication_estimates/Table1.xls", append
	*IV, baseline 
	ivreg2 deltadom20032015 (logexposure_post_pp=logexposure_pre_pp) [aweight=pop2003], ro first
	outreg2 using "$replication_estimates/Table1.xls", append
	
	*IV, controls + alternative specifications 
	
	*baseline
	ivreg2 deltadom20032015 (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier [aweight=pop2003], ro first
	outreg2 using "$replication_estimates/Table1.xls", append
	
	ivreg2 deltadom20032015 (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier [aweight=pop2003], ro first cl(cl)
	outreg2 using "$replication_estimates/Table1.xls", append

	
	 *Falsification (placebo)
	ivreg2 deltadom19932003 (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier [aweight=pop2003], ro first
	outreg2 using "$replication_estimates/Table1.xls", append
	ivreg2 deltadom20002003 (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier [aweight=pop2003], ro first
	outreg2 using "$replication_estimates/Table1.xls", append
	
	*other LLM components 
	ivreg2 logchgemployment (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier [aweight=pop2003], ro first
	outreg2 using "$replication_estimates/Table1.xls", append
	ivreg2 logchgpop (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier [aweight=pop2003], ro first
	outreg2 using "$replication_estimates/Table1.xls", append
	ivreg2 deltachom20032015 (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier [aweight=pop2003], ro first
	outreg2 using "$replication_estimates/Table1.xls", append
	ivreg2 deltachombis20032015 (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier [aweight=pop2003], ro first
	outreg2 using "$replication_estimates/Table1.xls", append
  
	
	
	*additional controls
	
	ivreg2 deltadom20032015 (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier [aweight=totemp2003], ro first
	outreg2 using "$replication_estimates_a/TableD19.xls", replace
	
    ivreg2 deltadom20032015 (logexposure_post_pp=logexposure_pre_pp)  [aweight=totemp2003], ro first
	outreg2 using "$replication_estimates_a/TableD19.xls", append

	ivreg2 deltadom20032015 (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier s_nms [aweight=pop2003], ro first
	outreg2 using "$replication_estimates_a/TableD19.xls", append

    ivreg2 deltadom20032015 (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier s_nms inpop [aweight=pop2003], ro    first
	outreg2 using "$replication_estimates_a/TableD19.xls", append
	
	
	ivreg2 deltadom20032015 (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier logother s_nms inpop [aweight=pop2003], ro first
	outreg2 using "$replication_estimates_a/TableD19.xls", append
	
	ivreg2 deltadom20032015 (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier, ro first
	outreg2 using "$replication_estimates_a/TableD19.xls", append

   	
	*Clemens& Hust ratio correction
    ivreg2 deltadom20032015 (logexposure_post_pp=lognbsal2003) logemp2003 s_postable2003 s_ind2003 s_ouvrier  [aweight=pop2003], ro first	
	outreg2 using "$replication_estimates_a/TableD19.xls", append
	
    ivreg2 deltadom20032015 (logexposure_post_pp=lognbsal2003) logemp2003 [aweight=pop2003], ro first	
	outreg2 using "$replication_estimates_a/TableD19.xls", append

    ivreg2 deltadom20032015 (logexposure_post_pp=lognbsal2003) s_postable2003 s_ind2003 s_ouvrier logemp2003, ro first		
	outreg2 using "$replication_estimates_a/TableD19.xls", append
	
	ivreg2 deltadom20032015 (logexposure_post_pp=lognbsal2003) logemp2003 s_postable2003 s_ind2003 s_ouvrier s_nms inpop [aweight=pop2003], ro first	
	outreg2 using "$replication_estimates_a/TableD19.xls", append
	
	g logdistance=log(distancenms2)	
	
    ivreg2 deltadom20032015 (logexposure_post_pp=logdistance) [aw=pop2003], ro first	
	outreg2 using "$replication_estimates_a/TableD19.xls", append

    ivreg2 deltadom20032015 (logexposure_post_pp=logdistance) s_postable2003 s_ind2003 s_ouvrier [aw=pop2003], ro first
	outreg2 using "$replication_estimates_a/TableD19.xls", append
	
    ivreg2 deltadom20032015 (logexposure_post_pp=logdistance) s_postable2003 s_ind2003 s_ouvrier s_nms inpop [aw=pop2003], ro first
	outreg2 using "$replication_estimates_a/TableD19.xls", append
	
	
	
    ivreg2 deltadom20032015 (exposure_post_pp=logdistance) s_postable2003 s_ind2003 s_ouvrier inpop [aw=pop2003], ro first
	outreg2 using "$replication_estimates_a/TableD19.xls", append
	

	
	
	

    
	*TABLE D14 
	*exposure measure to interprete 
	egen rank1=xtile(exposure_pre_pp), nq(10)
	egen rank2=xtile(exposure_post_pp), nq(10)
	
	sum exposure_pre_pp [aweight=pop2003],d 
	sum exposure_post_pp [aweight=pop2003],d 
	
	su exposure_post_pp if rank2==10 [aweight=pop2003]
	su exposure_post_pp if rank2<5 [aweight=pop2003]
	
	*By initial exposure to posting: TABLE D15
	
	su exposure_pre_pp if rank1==10 [aweight=pop2003]
	su exposure_pre_pp if rank1<5 [aweight=pop2003]
	
	su exposure_post_pp if rank1==10 [aweight=pop2003]
	su exposure_post_pp if rank1<5 [aweight=pop2003]
	
	su s_ind2003 if rank1==10 [aw=pop2003] 
	su s_ind2003 if rank1<5 [aw=pop2003] 
		
	su s_ouvrier if rank1==10 [aw=pop2003] 
	su s_ouvrier if rank1<5 [aw=pop2003] 	
	
	su s_postable2003 if rank1==10 [aw=pop2003] 
	su s_postable2003 if rank1<5 [aw=pop2003] 	
	
	su chomrel2003 if rank1==10 [aw=pop2003] 
	su chomrel2003 if rank1<5 [aw=pop2003] 	
	
	su pop2003 if rank1==10 [aw=pop2003] 
	su pop2003 if rank1<5 [aw=pop2003] 
	
	su frontalier2003 if rank1==10 [aw=pop2003] 
	su frontalier2003 if rank1<5 [aw=pop2003] 		
	
	******
	su exposure_pre_pp if rank2==10 [aweight=pop2003]
	su exposure_pre_pp if rank2<5 [aweight=pop2003]
	
	su exposure_post_pp if rank2==10 [aweight=pop2003]
	su exposure_post_pp if rank2<5 [aweight=pop2003]
	
	su s_ind2003 if rank2==10 [aw=pop2003] 
	su s_ind2003 if rank2<5 [aw=pop2003] 
		
	su s_ouvrier if rank2==10 [aw=pop2003] 
	su s_ouvrier if rank2<5 [aw=pop2003] 	
	
	su s_postable2003 if rank2==10 [aw=pop2003] 
	su s_postable2003 if rank2<5 [aw=pop2003] 	
	
	su chomrel2003 if rank2==10 [aw=pop2003] 
	su chomrel2003 if rank2<5 [aw=pop2003] 	
	
	su pop2003 if rank2==10 [aw=pop2003] 
	su pop2003 if rank2<5 [aw=pop2003] 
	
	su frontalier2003 if rank2==10 [aw=pop2003] 
	su frontalier2003 if rank2<5 [aw=pop2003] 		


	
	
	*baseline: OLS/RF
	reg deltadom20032015 exposure_post_pp [aweight=pop2003], ro first
	outreg2 using "$replication_estimates_a/TableD18.xls", replace
	reg deltadom20032015 exposure_post_pp s_postable s_ind s_ouvrier [aweight=pop2003], ro first
	outreg2 using "$replication_estimates_a/TableD18.xls", append
	reg deltadom20032015 exposure_post_pp s_postable s_ind s_ouvrier, ro first
	outreg2 using "$replication_estimates_a/TableD18.xls", append
	reg deltadom20032015 exposure_pre_pp [aweight=pop2003], ro first
	outreg2 using "$replication_estimates_a/TableD18.xls", append
	reg deltadom20032015 exposure_pre_pp s_postable s_ind s_ouvrier [aweight=pop2003], ro first
	outreg2 using "$replication_estimates_a/TableD18.xls", append
	reg deltadom20032015 exposure_pre_pp s_postable s_ind s_ouvrier, ro first
	outreg2 using "$replication_estimates_a/TableD18.xls", append
	
	*IV
	ivreg2 deltadom20032015 (exposure_post_pp=logexposure_pre_pp) [aweight=pop2003], ro first
	outreg2 using "$replication_estimates_a/TableD18.xls", append
	ivreg2 deltadom20032015 (exposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier [aweight=pop2003], ro first
	outreg2 using "$replication_estimates_a/TableD18.xls", append
	ivreg2 deltadom20032015 (exposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier, ro first
	outreg2 using "$replication_estimates_a/TableD18.xls", append
	
	
	
	
	* pre-characteristics
	g s_ouvrier_100=s_ouvrier*100
	g s_ind_100=s_ind2003*100
	
	reg logexposure_pre_pp s_ouvrier_100, ro 
	reg logexposure_pre_pp s_ind_100, ro 
	reg logexposure_pre_pp inpop, ro 
	reg logexposure_pre_pp inpop if Region!="Ile-de-France", ro 
	reg exposure_pre_pp s_ind_100, ro 
	reg exposure_pre_pp s_ouvrier_100, ro 
	reg exposure_pre_pp inpop, ro 
	
	*zero first stage 
	
	g logchgemployment93=(log(employment2003)-log(employment1993))
	g logchgpop93=(log(pop2003)-log(pop1993))
   	g logchom93=(log(T12003)-log(T11993))
 	g FDunemp=log((T12015/100)*pop2015)- log((T12003/100)*pop2003)

	
	reg logchgemployment93 logexposure_pre_pp, ro 
	outreg2 using "$replication_estimates_a/TableD17.xls", replace
	reg logchgemployment93 logexposure_pre_pp [aw=pop2003], ro 
	outreg2 using "$replication_estimates_a/TableD17.xls", append
	reg logchgemployment93 exposure_pre_pp [aw=pop2003], ro 
	outreg2 using "$replication_estimates_a/TableD17.xls", append
	reg logchgpop93 logexposure_pre_pp, ro 
	outreg2 using "$replication_estimates_a/TableD17.xls", append
	reg logchgpop93 logexposure_pre_pp [aw=pop1993], ro 
	outreg2 using "$replication_estimates_a/TableD17.xls", append
	reg logchgpop93 exposure_pre_pp [aw=pop2003], ro 
	outreg2 using "$replication_estimates_a/TableD17.xls", append
	reg logchom93 logexposure_pre_pp, ro 
	outreg2 using "$replication_estimates_a/TableD17.xls", append
	reg logchom93 logexposure_pre_pp [aw=pop2003], ro 
	outreg2 using "$replication_estimates_a/TableD17.xls", append
	reg logchom93 logexposure_pre_pp [aw=pop1993], ro 
	outreg2 using "$replication_estimates_a/TableD17.xls", append

	*leaveout test 
	
	eststo: ivregress 2sls deltadom20032015 (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier if code_insee!=1 [aweight=pop2003], robust 
	outreg2 using "$replication_estimates_a/fig5leaveout.xls", replace
	forval x=2/19{
	eststo: ivregress 2sls deltadom20032015 (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier if code_insee!=`x' [aweight=pop2003],  robust 
	outreg2 using "$replication_estimates_a/leaveout.xls",  append	
	}
	
	forval x=21/94{
	eststo: ivregress 2sls deltadom20032015 (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier if code_insee!=`x' [aweight=pop2003],  robust 
	outreg2 using "$replication_estimates_a/leaveout.xls",  append	
	}
	save "$data/FD_Figure5.dta", replace 
	
	
	***********
	use "$data/panel_Figure5.dta", clear
	keep if year==2003 | year==2015 | year==1993 | year==2000 | year==1999 | year==1990
	*keep only employment in sheltered sectors
	keep if postable==0
	rename nbsal2003 posting_pre
	drop g*  log* s_postable s_industrie chomagenb nbsalmeand2
	
	reshape wide employment pop emprel totemp chomrel T1 nbsal* frontalier, i(code_insee ) j(year)	
	
	g deltadom20032015=(emprel2015-emprel2003)
	foreach y in exposure_post_pp exposure_post_pp2 exposure_pre_pp{
	g log`y'=log(`y')	
	}
	g logchgemployment_sheltered=(log(employment2015)-log(employment2003))*100

	
	*other LLM components 
	ivreg2 logchgemployment_sheltered (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier  [aweight=pop2003], ro first
	outreg2 using "$replication_estimates/Table1.xls", append
	ivreg2 logchgemployment_sheltered (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier  [aweight=pop2003], ro first
	outreg2 using "$replication_estimates/Table1.xls", append


	*********************************************************
	* Decomposition of "domestic" employment by citizenship
	*********************************************************
	
	 
	   *SAPHIRE micro-level historical (decadal) censuses data collapsed at the citizenship-exposed sector-province-year level 
	   
	   use "/Users/Mathilde/Dropbox/Pensioners_in_SpatialEq/Data/Country-level/France/rp19682016.dta", clear
       drop if nes4=="9"
       g for=0
       replace for=1 if natio=="000"
       replace for=1 if natio=="001"
       collapse (sum) pond, by(nes4 for an_recens dep_res_18)
	   save "$data/rp_panel_for.dta", replace
	   
	   
	   
	   use "$data/rp_panel_for.dta", clear 
	   destring an_recens, replace
	   drop if dep_res_18=="2A"
	   drop if dep_res_18=="2B"
	   destring dep_res_18, replace
	   keep if nes=="1" | nes=="2" | nes=="3"
	   collapse (sum) pond, by(for an_recens dep_res_18)
	   rename an_recens year
	   keep if year==1999 | year==2016 | 1990
	   reshape wide pond, i(dep_res_18 year) j(for)
	   reshape wide pond0 pond1, i(dep_res_18) j(year)
	   save "$data/rp_panel_resh.dta", replace
	   
	   
	  

	   use "$data/rp_panel_resh.dta", replace
       rename dep_res_18 code_insee 
	   merge 1:1 code_insee using "$data/FD_Figure5.dta"
	   keep if _m==3
	  
	   egen emp1999_cens=rowtotal(pond11999 pond01999)
	   egen emp2016_cens=rowtotal(pond12016 pond02016)
	   egen emp1990_cens=rowtotal(pond11990 pond01990)
   
	   order employment2003 emp1999_cens employment2015 emp2016_cens
	   
	   foreach x in 1999 1990{
	   g mig`x'=(pond0`x'/emp`x'_cens)*employment`x'
	   g dom`x'=(pond1`x'/emp`x'_cens)*employment`x'
	   }
	   
	   g mig2015=(pond02016/emp2016_cens)*employment2015
	   g dom2015=(pond12016/emp2016_cens)*employment2015
	   g mig2003=(pond01999/emp1999_cens)*employment2003
	   g dom2003=(pond11999/emp1999_cens)*employment2003

	   
	   g crosschech=(mig2015+dom2015)/pop2015*100-(mig1999+dom1999)/pop1999*100
	   
	   
	   
	   g deltamig=(mig2015)/pop2015*100-(mig2003)/pop2003*100
	   g deltadom=(dom2015)/pop2015*100-(dom2003)/pop2003*100
	   
	   ****placebo***
	   g deltamig99=(mig2003)/pop2003*100-(mig1990)/pop1990*100
	   g deltadom99=(dom2003)/pop2003*100-(dom1990)/pop1990*100
	   
	   
	  ivreg2 deltadom20032015 (logexposure_post_pp=logexposure_pre_pp) [aweight=pop2003], ro first	   

	   
	   
	   *OLS, level 
	   reg deltadom20032015 exposure_post_pp [aweight=pop2003], ro
	   reg deltamig exposure_post_pp [aweight=pop2003], ro
	   reg deltadom exposure_post_pp [aweight=pop2003], ro
      
	   reg deltadom20032015 exposure_post_pp s_postable2003 s_ind2003 s_ouvrier  [aweight=pop2003], ro
	   reg deltamig exposure_post_pp s_postable2003 s_ind2003 s_ouvrier  [aweight=pop2003], ro
	   reg deltadom exposure_post_pp s_postable2003 s_ind2003 s_ouvrier  [aweight=pop2003], ro
	   
	  

	    
	   reg deltadom20032015 logexposure_post_pp [aweight=pop2003], ro
	   reg deltamig logexposure_post_pp [aweight=pop2003], ro
	   reg deltadom logexposure_post_pp [aweight=pop2003], ro
       
	   reg deltadom20032015 logexposure_post_pp s_postable2003 s_ind2003 s_ouvrier  [aweight=pop2003], ro
	   reg deltamig logexposure_post_pp s_postable2003 s_ind2003 s_ouvrier  [aweight=pop2003], ro
	   reg deltadom logexposure_post_pp s_postable2003 s_ind2003 s_ouvrier  [aweight=pop2003], ro
	   
	  
	 
   
	  
	   ivreg2 deltadom20032015 (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier [aweight=pop2003], ro first	
	   outreg2 using "$replication_estimates_a/TableD20.xls", replace
       ivreg2 deltadom (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier  [aweight=pop2003], ro first	   
 	   outreg2 using "$replication_estimates_a/TableD20.xls", append      
	   ivreg2 deltamig (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier  [aweight=pop2003], ro first	   
	  outreg2 using "$replication_estimates_a/TableD20.xls", append      
   
	   *pre-trends 
	   ivreg2 deltamig99 (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier  [aweight=pop2003], ro first	
 	   outreg2 using "$replication_estimates_a/TableD20.xls", append      
 	   ivreg2 deltadom99 (logexposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier  [aweight=pop2003], ro first	
 	   outreg2 using "$replication_estimates_a/TableD20.xls", append      
	   
	   
	   
	   ivreg2 deltadom20032015 (exposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier [aweight=pop2003], ro first	
	   outreg2 using "$replication_estimates_a/TableD20.xls", append
       ivreg2 deltadom (exposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier  [aweight=pop2003], ro first	   
 	   outreg2 using "$replication_estimates_a/TableD20.xls", append      
	   ivreg2 deltamig (exposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier  [aweight=pop2003], ro first	   
	  outreg2 using "$replication_estimates_a/TableD20.xls", append      
   
	   *pre-trends 
	   ivreg2 deltamig99 (exposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier  [aweight=pop2003], ro first	
 	   outreg2 using "$replication_estimates_a/TableD20.xls", append      
 	   ivreg2 deltadom99 (exposure_post_pp=logexposure_pre_pp) s_postable2003 s_ind2003 s_ouvrier  [aweight=pop2003], ro first	
 	   outreg2 using "$replication_estimates_a/TableD20.xls", append  
	   
		
	  
	  
	  
	  ****************************************************************
	  * Control for a province exposure to nationwide demand shocks in exposed sectors 
	  *****************************************************************
	  
	  
	  
	use "$data/merge_employment_FR_19892015.dta", clear 
	drop if year>2015
	egen totnbsal=sum(nbsal), by(sector)
	g postable=1
	replace postable=0 if totnbsal==0
	keep if postable==1
	keep if year==2003
	egen tot=sum(employment), by(sector)
	g share=employment/tot
	rename share share_2003 
	keep share_2003 sector code_insee 
	save "$data/postable_initial_shares.dta", replace
	
	
	
	use "$data/merge_employment_FR_19892015.dta", clear 
	egen totnbsal=sum(nbsal), by(sector)
	g postable=1
	replace postable=0 if totnbsal==0
    keep if postable==1
	egen tot=sum(employment), by(sector year)
	cap drop _m
	merge m:1 code_insee sector using "$data/postable_initial_shares.dta"
	*a province exposure to nationwide "shock" is a 2003 province share * yearly French employment in those sectors (varies over time)
	g emp_shock=share_2003*tot 
	g init2003=tot if year==2003
	egen sinit2003=min(init2003), by(sector)
	*alternative:  the 2003 province share * French employment in those sectors relative to 2003
	g emp_shock_pp=share_2003*(tot-sinit2003)
	collapse (sum) emp_shock emp_shock_pp (mean) tot sinit2003, by(code_insee year)
	save "$data/postable_emp_shocks.dta", replace
	
	
	
	use "$data/panel_Figure5.dta", clear 
	merge m:1 code_insee year using "$data/postable_emp_shocks.dta"
	drop if _m==2
	*******
	g deltaemp=emp_shock/totemp2003
	g deltaemp_pp=emp_shock_pp/totemp2003
	*******
	foreach x in exposure_pre_pp{
	forval y=1990/2015{
	g g`y'=0 
	replace g`y'=1  if year==`y'
	replace g`y'=g`y'*`x'
	label var g`y' "`y'"
	}
	}
    g zero=0
	
	reghdfe logemployment g1994-g2002 zero g2004-g2015 if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee year) cluster(code_insee)
	est store baseline
	reghdfe logemployment g1994-g2002 zero g2004-g2015 deltaemp if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee year) cluster(code_insee)
	est store ss1 
	reghdfe logemployment g1994-g2002 zero g2004-g2015 deltaemp if year>1993 & postable==1 [aweight=pop2003], absorb(code_insee year c.s_ind2003##year c.s_ouvrier##year c.s_nms##year) cluster(code_insee)
    est store ss2
	
	coefplot (baseline, label("Baseline (Province, Year FE)") ) ///
	(ss1, label("+ Shift-Share Control") lcolor(gs10) mcolor(gs10) ciopts(lcolor(gs10))) ///
	(ss2, label("Shift-Share Control, controls x year FE") lcolor(eltblue) mcolor(eltblue) ciopts(lcolor(eltblue))) ///
	, keep( g1994 g1995 g1996 g1997 g1998 g1999 g2000 g2001 g2002 zero g2004 g2005 g2006 g2007 g2008 g2009 g2010 g2011 g2012 g2013 g2014     g2015) vertical omit ///
	 graphregion(fcolor(white) lcolor(white)) coeflabels(,angle(45)) yline(0, lcolor(gs10) lpattern(dash)) ///
	 xline(10.5, lcolor(red)) recast(connected) ///
	 ytitle("Coeff on Year*Exposure Index") legend(cols(1) ring(0) pos(7) size(small)) 	ylabel(-.1 (.1) .02)
     graph export "$replication_outputs_a/FigureD43e.pdf", replace	
	 
	drop _m
	keep if year==2003 | year==2015 
	*keep only employment in exposed sectors
	keep if postable==1
	drop nbsal2003
	drop g*  log* s_postable s_industrie chomagenb nbsalmeand2 tot
	reshape wide employment pop delta* emprel totemp chomrel T1 nbsal* frontalier emp_shock* , i(code_insee ) j(year)	
 
	g deltadom20032015=(emprel2015-emprel2003)
	foreach y in exposure_post_pp exposure_post_pp2 exposure_pre_pp{
	g log`y'=log(`y')	
	}
	g logchgemployment=(log(employment2015)-log(employment2003))*100
	g logchgtotemployment=(log(totemp2015)-log(totemp2003))*100
	g logchgpop=(log(pop2015)-log(pop2003))*100

    ivreg2 deltadom20032015 deltaemp_pp2015 (logexposure_post_pp=logexposure_pre_pp) [aweight=pop2003], ro first	 
	outreg2 using "$replication_estimates_a/TableD19.xls", append
    ivreg2 deltadom20032015 deltaemp_pp2015 s_postable s_ind2003 s_ouvrier (logexposure_post_pp=logexposure_pre_pp) [aweight=pop2003], ro first	   
	outreg2 using "$replication_estimates_a/TableD19.xls", append
	
	****************************************************
	*. Raw levels 
	****************************************************

	use "$data/merge_employment_FR_19892015.dta", clear 
	cap drop _m
	merge m:1 code_insee using "$data/initialexposure.dta", keepusing(nbsal2003 Region)
	drop _m 
	drop if year>2015 
	egen totnbsal=sum(nbsal), by(sector)
	g postable=1
	replace postable=0 if totnbsal==0
	*constant variable at the province-level
	egen totemp=sum(employment), by(code_insee year)
	g totemp2003d=totemp if year==2003
	egen totemp2003=min(totemp2003d), by(code_insee)
	gen exposure_pre_pp=(nbsal2003/totemp2003)*100
	
	collapse (sum) employment (mean) pop  exposure_pre_pp, by(postable code_insee year)
	
	
	*Rank of pre-existing exposure 
	egen rankrelempd=xtile(exposure_pre_pp) if postable==1 & year==2004, nq(10) 
	egen rankrelemp=min(rankrelempd), by(code_insee)
	g top=.
	replace top=1 if rankrelemp>9 & rankrelemp!=.
	replace top=0 if rankrelemp<5 & rankrelemp!=.
	drop if top==.
	
    collapse (sum) employment pop, by(postable top year)
	
	g logemp=log(employment)
	g emprel=employment/pop
	foreach y in employment emprel{
	g normp`y'=`y' if year==2003
	egen norm`y'=min(normp`y'), by(top postable)
	g normemp`y'=`y'/norm`y'
	}

	g post=0
	replace post=1 if year>2003
	g interac=0
	replace interac=1 if postable==1 & top==1 & post==1
	g logemprel=log(emprel)

	reg logemprel i.top i.year i.top##i.post if postable==1 & year>1993, ro
    global beta=round(_b[1.top#1.post],.01)
	global se=round(_se[1.top#1.post],.01)

	twoway (connected normempemprel year if top==1 & postable==1 & year>1993, lcolor(cranberry) mcolor(cranberry)) ///
	(connected normempemprel year if top==0 & postable==1 & year>1993, lcolor(dknavy) mcolor(dknavy)), ///
	legend(label(1 "Top 10% Exposure") label(2 "Bottom 40% Exposure") cols(1) ring(0) pos(11)) ylabel(0.7 (0.3) 1.3) ///
	graphregion(fcolor(white) lcolor(white)) ///
	xlabel(1994 (1) 2015, angle(vertical)) xtitle("") ytitle("Share of Working Age Pop. in Postable Sectors (2003=1)") ///
	xline(2004, lcolor(red)) ///
	ttext(1.3 2010 "DiD: $beta($se)", place(e))
    graph export "$replication_outputs_a/FigureD35.pdf", replace	
	
	reg logemp i.top i.year i.top##i.post if postable==1 & year>1993, ro
    global beta=round(_b[1.top#1.post],.001)
	global se=round(_se[1.top#1.post],.01)
	
	twoway (connected normempemployment year if top==1 & postable==1 & year>1993, lcolor(cranberry) mcolor(cranberry)) ///
	(connected normempemployment year if top==0 & postable==1 & year>1993, lcolor(dknavy) mcolor(dknavy)), ///
	legend(label(1 "Top 10% Exposure") label(2 "Bottom 40% Exposure") cols(1) ring(0) pos(11)) ylabel(0.7 (0.3) 1.3) ///
	graphregion(fcolor(white) lcolor(white)) ///
	xlabel(1994 (1) 2015, angle(vertical)) xtitle("") ytitle("Share of Working Age Pop. in Postable Sectors (2003=1)") ///
	xline(2004, lcolor(red)) ///
	ttext(1.3 2010 "DiD: $beta($se)", place(e))
     graph export "$replication_outputs_a/FigureD36e.pdf", replace	
	
	
	reg logemprel i.top i.year i.top##i.post if postable==0 & year>1993, ro
    global beta=round(_b[1.top#1.post],.001)
	global se=round(_se[1.top#1.post],.01)

	twoway (connected normempemprel year if top==1 & postable==0 & year>1993, lcolor(cranberry) mcolor(cranberry)) ///
	(connected normempemprel year if top==0 & postable==0 & year>1993, lcolor(dknavy) mcolor(dknavy)), ///
	legend(label(1 "Top 10% Exposure") label(2 "Bottom 40% Exposure") cols(1) ring(0) pos(11)) ylabel(0.7 (0.3) 1.3) ///
	graphregion(fcolor(white) lcolor(white)) ///
	xlabel(1994 (1) 2015, angle(vertical)) xtitle("") ytitle("Share of Working Age Pop. in Non-Postable Sectors (2003=1)") ///
	xline(2004, lcolor(red)) ///
	ttext(1.3 2010 "DiD: $beta($se)", place(e))

	
	reg logemp i.top i.year i.top##i.post if postable==0 & year>1993, ro
    global beta=round(_b[1.top#1.post],.0001)
	global se=round(_se[1.top#1.post],.01)
	
    twoway (connected normempemployment year if top==1 & postable==0 & year>1993, lcolor(cranberry) mcolor(cranberry)) ///
	(connected normempemployment year if top==0 & postable==0 & year>1993, lcolor(dknavy) mcolor(dknavy)), ///
	legend(label(1 "Top 10% Exposure") label(2 "Bottom 40% Exposure") cols(1) ring(0) pos(11)) ylabel(0.7 (0.3) 1.3) ///
	graphregion(fcolor(white) lcolor(white)) ///
	xlabel(1994 (1) 2015, angle(vertical)) xtitle("") ytitle("Share of Working Age Pop. in Non-Postable Sectors (2003=1)") ///
	xline(2004, lcolor(red)) ///
	ttext(1.3 2010 "DiD: $beta($se)", place(e))

	
	

	



